package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.expand.ParkUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.expand.ParkUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.ParkUploadDao;
import io.renren.modules.basedata.entity.tbl_park;
import io.renren.modules.basedata.service.ParkUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ParkUploadServiceImpl implements ParkUploadService {

    private static final Logger log = LoggerFactory.getLogger(ParkUploadServiceImpl.class);

    @Autowired
    private ParkUploadDao parkUploadDao;

    @Override
    public String parkUpload() throws Exception {
        log.info("开始获取停车场信息！");
        int operResult = 0;
        List<tbl_park> list = null;
        tbl_park park = null;
        list = getParkList();//获取停车场信息
        if(list!=null&&list.size()>0){
            for(int i=0;i<list.size();i++){
                park = list.get(i);
                //停车场信息上传及变更接口
                ParkUploadRequest request = new ParkUploadRequest();
                request.setId(park.getId());
                request.setName(park.getName());
                request.setParkOperatorId(park.getParkOperatorId());
                request.setAddress(park.getAddress());
                request.setLat(""+park.getLat());
                request.setLng(""+park.getLng());
                request.setProvinceNum(park.getProvinceNum());
                request.setStartTime(DateUtil.formatDate(park.getStartTime()));
                request.setEndTime(DateUtil.formatDate(park.getEndTime()));
                request.setOperation(park.getFlag());

                //获取发送文件名
                String fileName = ETCCommHelper.getFileName(request, "65");  //发送方编码（新疆--65）
                try{
                    //指定响应的协议模型，调用upload发送数据
                    ParkUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, ParkUploadResponse.class);
                    log.info("上传停车场信息成功，服务器返回："+response.toJson());
                    if(response!=null&&response.getInfo().equals("成功！")){
                        //更新上传标志
                        if(parkUploadDao.updatePark(park.getId())==1){
                            log.info("更新停车场信息表上传标志成功!");
                            operResult = 1;
                        }else{
                            log.info("更新停车场信息表上传标志成功!");
                        }
                    }else{
                        log.info("上传停车场信息失败!失败原因："+response.getInfo());

                        parkUploadDao.updateParkError(park.getId());
                    }
                }catch(IOException e){
                    log.error("上传停车场信息失败！错误信息："+e.getMessage());

                    parkUploadDao.updateParkError(park.getId());
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传停车场信息失败！错误信息："+apie.getMessage());
                    continue;
                }
            }
            return "上传停车场信息结束！";
        }else{
            return "无停车场信息，上传结束...";
        }
    }

    //获取停车场信息
    @SuppressWarnings("unchecked")
    public List<tbl_park> getParkList() throws IOException{
        List<tbl_park> list;
        try{
            list = parkUploadDao.getParkList();
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取停车场信息失败！错误信息：",e);
            throw new IOException("获取停车场信息失败:"+e.getMessage());
        }
        return list;
    }
}
